﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using OnlineGameLibrary.ClientChannel;
using System.ServiceModel.Channels;
using OnlineGameLibrary.MessageEx;

namespace OnlineGameLibrary.Activities
{

    public sealed class ClientChannelReceive : AsyncCodeActivity<Message>
    {
        [RequiredArgument]
        public InArgument<MessageDefine> MessageDefine { set; get; }

        [RequiredArgument]
        public InArgument<IMyClientChannel> ClientChannel { get; set; }

        private IAsyncResult _ar;
        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {
            var define = MessageDefine.Get(context);
            _ar = ClientChannel.Get(context).BeginReceiveMessageFromPlayer(define, callback, state);
            return _ar;
        }

        protected override Message EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
        {
            return ClientChannel.Get(context).EndReceiveMessageFromPlayer(result);
        }

        protected override void Cancel(AsyncCodeActivityContext context)
        {
            ClientChannel.Get(context).CancelReceiveMessageFromPlayer(_ar);
            base.Cancel(context);
        }
    }
}
